import pymysql
from time import sleep

class MysqlDB(object):

    #设置全局变量
    #登录账号

    def LoginUser(self,id,passwd):
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='pythondb', charset='utf8')
        result =False
        rsor = conn.cursor()
        n=0
        try:
            print("test")
            n = rsor.execute("select UserID,Paw from login"
                             " WHERE UserID='%s' and Paw='%s'" % (id, passwd))
        except:
            print("账号或者密码不正确！！！")

        if n>0:
            result=True
        rsor.close()
        conn.close()
        return result

    #注册码注销
    def cencel_code(self,rsor,m,codekey):
        key = 0
        print(m)
        print("tt")
        if m == 1:                       # 结果row=1时，证明没有被使用的注册码，注册码有效
            # try:
            sql_2 = "update registrationcode set Code=1 WHERE Registration=(%s,)"
            rsor.execute("update registrationcode set Code=0 WHERE Registration='%s'" %(codekey))
            print("注册码注销使用成功")
            key = 1
            # except:
            #     print("注册码注销失败")
        elif m == 2:
            print("取值失败")
        else:
            print("注册码已经失效")
        #传递 数据库对象 和 key
        list=[]
        list.append(rsor)
        list.append(key)
        return list




    #对使用过的注册码进行注销   __*****报废状态
    def cencel(self,rsor,codekey):
        result=False
        key=0
        returnlist=[]
        print("运行cencel")
        # try:
        sql_1="select Code from registrationcode WHERE Registration=(%s)"
        rsor.execute("select Code from registrationcode WHERE Registration='%s'" % (codekey))
        print("查询成功")
        m=2
        for r in rsor:
            print(r)
            m=int(r[0])
            # m=1
        print(m)
        print("test")
        returnlist=self.cencel_code(rsor,m,codekey)
        key=returnlist[1]
        # except:
        #     print("注册码错误")
        if key==1:
            result=True
        # 传递 数据库对象 和 result
        list = []
        rsor=returnlist[0]
        list.append(rsor)
        list.append(result)
        return list


    #注册账号
    def Register(self,id,passwd,codekey):
        returnlist=[]
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='pythondb', charset='utf8')
        result=False
        rsor=conn.cursor()
        n=0
        try:
            sql="insert into login VALUES (%s,%s,%s)"
            n = rsor.execute(sql, (id, passwd, codekey))
        except:
            print("注册失败,输入不正确，或者账号已经注册")
        if n>0:
            returnlist=self.cencel(rsor,codekey)
            result_code=returnlist[1]
            rsor=returnlist[0]

            if result_code==True:
                result = True
        try:
            rsor.close()
            if result==True:
                conn.commit()       #提交事务
        except:
            rsor.close()
            conn.rollback()
        return result


    #注册码生成
    def RegisterCreate(codekey):

        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='pythondb', charset='utf8')
        result = False
        rsor = conn.cursor()
        n=0
        try:
            sql="insert into registrationcode VALUES (%s,%s)"
            n=rsor.execute(sql,(codekey,'1'))
        except:
            print("注册生成失败")
        if n>0:
            result=True
        rsor.close()
        conn.commit()
        conn.close()
        return result








